package com.lilang.test;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    private EditText etName;
    private EditText etPassword;
    private EditText etSex;
    private Button login;
    private Button register;
    SQLiteHelper sqLiteHelper;
    int flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        etName = (EditText) findViewById(R.id.et_name);
        etPassword = (EditText) findViewById(R.id.et_password);
        etSex = (EditText) findViewById(R.id.et_sex);
        login = (Button) findViewById(R.id.login);
        register = (Button) findViewById(R.id.register);

        login.setOnClickListener(this);
        register.setOnClickListener(this);
        sqLiteHelper=new SQLiteHelper(this);
    }

    @Override
    public void onClick(View v) {
        int version = sqLiteHelper.Version();
        Log.d(TAG, "onClick: "+version);
        SQLiteDatabase db = sqLiteHelper.getWritableDatabase();
        String name=etName.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String sex = etSex.getText().toString().trim();
        switch (v.getId()) {
            case R.id.login:
                Method(name,password,"","login",db);
                break;
            case R.id.register:
                if (version==1){
                    Method(name,password,"","register",db);
                }else{
                    etSex.setVisibility(View.VISIBLE);
                    Method(name,password,sex,"register",db);
                }

                break;
        }

        sqLiteHelper.close();
    }


    /**
     * 此方法用于注册和登陆账户
     * @param name 用户名
     * @param password 密码
     * @param vname 标识
     * @param db SQLiteDatabase
     */
    public void Method(String name,String password,String sex,String vname,SQLiteDatabase db){
        if ("".equals(name)||"".equals(password)){
            Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
        }else{
            try {
                if ("login".equals(vname)){
                    Cursor cursor = db.rawQuery("select * from user where username=? and password=?", new String[]{name,password});
                    flag=cursor.getCount();
                } else if("register".equals(vname)){
                    db.execSQL("insert into user(username,password,sex) values(?,?,?)",new String[]{name,password,sex});
                    flag=1;
                }
            }catch (Exception e){
                flag=0;
            }
            if (flag==1){
                etName.setText("");
                etPassword.setText("");
                etSex.setText("");
                if ("login".equals(vname)){
                    Intent intent=new Intent(MainActivity.this,SuccessActivity.class);
                    startActivity(intent);
                }else if("register".equals(vname)){
                    etSex.setVisibility(View.GONE);
                    Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                }
            }else{
                if ("login".equals(vname)){
                    Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }else if("register".equals(vname)){
                    Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
